package com.ymatou.iapi.fundcommission.testcase;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.fundcommission.service.DetailReportExportInfoListGetCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.sqlwapper.YmtFundDimensionWapper;
import com.ymttest.utils.JsonBeanHelper;
/**
*交易服务费详情下载接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_DetailReportExportInfoListGet{
	private static DetailReportExportInfoListGetCall detailreportexportinfolistgetCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("交易服务费详情下载接口");
	}

	@Before
	public void caseUp() {
		detailreportexportinfolistgetCall=new DetailReportExportInfoListGetCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(detailreportexportinfolistgetCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DetailReportExportInfoListGet_001() {
		Logger.start(true,"交易服务费详情下载（收佣）");
		try {
			//TODO 补全case代码
			//sql取出orderid
			YmtFundDimensionWapper ymtFundDimensionWapper = new YmtFundDimensionWapper();
	        List<Map> sqlfororderid = ymtFundDimensionWapper.commissionOrderbyBusinessType(11);
	        int userId = (int) sqlfororderid.get(0).get("SellerId");
	        String userLoginId = (String) sqlfororderid.get(0).get("SellerLoginId");
			String orderInfoIddate = (String) sqlfororderid.get(0).get("OrderInfoId");
			String businessTime = (String) sqlfororderid.get(0).get("BusinessTime");
			Logger.debug(businessTime);
			//参数传值
			Map<String,Object> calldate = new HashMap<String, Object>();	
			calldate.put("CommissionType", 1);	//1收佣 2退佣 3返佣	
			calldate.put("UserId", userId); 
			calldate.put("UserLoginId", userLoginId); 
			calldate.put("BeginTime", businessTime); 
			calldate.put("EndTime", businessTime); 				
			//call & 正向case断言
			//根据参数获取result
			String result = detailreportcall(calldate);
			validate_step1(result);	
			//业务层数据断言
			Map<String,Object> sqlm = new HashMap<String, Object>();
			sqlm.put("businessType", 11); 
			sqlm.put("orderInfoId", orderInfoIddate); 
			validate_step2(result, sqlm);				
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DetailReportExportInfoListGet_002() {
		Logger.start(true,"交易服务费详情下载（退佣）");
		try {
			//TODO 补全case代码
			//sql取出orderid
			YmtFundDimensionWapper ymtFundDimensionWapper = new YmtFundDimensionWapper();
	        List<Map> sqlfororderid = ymtFundDimensionWapper.commissionOrderbyBusinessType(21);
	        int userId = (int) sqlfororderid.get(0).get("SellerId");
	        String userLoginId = (String) sqlfororderid.get(0).get("SellerLoginId");
			String orderInfoIddate = (String) sqlfororderid.get(0).get("OrderInfoId");
			String businessTime = (String) sqlfororderid.get(0).get("BusinessTime");
			Logger.debug(businessTime);
			//参数传值
			Map<String,Object> calldate = new HashMap<String, Object>();	
			calldate.put("CommissionType", 2);	//1收佣 2退佣 3返佣	
			calldate.put("UserId", userId); 
			calldate.put("UserLoginId", userLoginId); 
			calldate.put("BeginTime", businessTime); 
			calldate.put("EndTime", businessTime); 				
			//call & 正向case断言
			//根据参数获取result
			String result = detailreportcall(calldate);
			validate_step1(result);	
			//业务层数据断言
			Map<String,Object> sqlm = new HashMap<String, Object>();
			sqlm.put("businessType", 21); 
			sqlm.put("orderInfoId", orderInfoIddate); 
			validate_step2(result, sqlm);				
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DetailReportExportInfoListGet_003() {
		Logger.start(true,"交易服务费详情下载（返佣）");
		try {
			//TODO 补全case代码
			//sql取出orderid
			YmtFundDimensionWapper ymtFundDimensionWapper = new YmtFundDimensionWapper();
	        List<Map> sqlfororderid = ymtFundDimensionWapper.commissionOrderbyBusinessType(12);
	        int userId = (int) sqlfororderid.get(0).get("SellerId");
	        String userLoginId = (String) sqlfororderid.get(0).get("SellerLoginId");
			String orderInfoIddate = (String) sqlfororderid.get(0).get("OrderInfoId");
			String businessTime = (String) sqlfororderid.get(0).get("BusinessTime");
			Logger.debug(businessTime);
			//参数传值
			Map<String,Object> calldate = new HashMap<String, Object>();	
			calldate.put("CommissionType", 3);	//1收佣 2退佣 3返佣	
			calldate.put("UserId", userId); 
			calldate.put("UserLoginId", userLoginId); 
			calldate.put("BeginTime", businessTime); 
			calldate.put("EndTime", businessTime); 				
			//call & 正向case断言
			//根据参数获取result
			String result = detailreportcall(calldate);
			validate_step1(result);	
			//业务层数据断言
			Map<String,Object> sqlm = new HashMap<String, Object>();
			sqlm.put("businessType", 12); 
			sqlm.put("orderInfoId", orderInfoIddate); 
			validate_step2(result, sqlm);				
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}	
	
	/**
	 * CALL DetailReportProductInfoGet
	 * @param param 请求参数
	 * @return 返回报文
	 * @throws Exception
	 */
	public static String  detailreportcall(Map param) throws Exception {
	    try {	    	
	    	//调用接口json文件
			JSONObject detailReportExportInfoListGetBean =  JsonBeanHelper.getJsonBean("/com.ymatou.iapi.fundcommission/DetailReportExportInfoListGet.json");
			detailReportExportInfoListGetBean = JsonBeanHelper.updateJsonBean(detailReportExportInfoListGetBean, param);				
			detailreportexportinfolistgetCall.setData(detailReportExportInfoListGetBean);
			detailreportexportinfolistgetCall.callService();
			String result = detailreportexportinfolistgetCall.getReturnData();
			return result;
	    }catch (Exception e) {  
	        e.printStackTrace();
	        
	    }  
	    return null;
	}
	/**
	 * 验证接口是否调用通过
	 * @param dataStr 返回报文
	 * @throws Exception
	 */
	public static void validate_step1(String dataStr) throws Exception {
	    try {	    	
			//返回报文断言
			Logger.verifyNotNull(dataStr, "返回报文非空");			
			String Success =  new JSONObject(dataStr).getString("Success");
			Logger.verifyIsContains(true, Success, "\"Success\":true");
			String errMsg =  new JSONObject(dataStr).getString("ErrMsg");
			Logger.verifyIsNull(errMsg, "Msg等于空");
			String errCode =  new JSONObject(dataStr).getString("ErrCode");
			Logger.verifyIsNull(errCode, "ErrCode等于空");
			String body =  new JSONObject(dataStr).getString("Body");
			Logger.verifyIsNull(body, "Body等于空");
			String requestUrl =  new JSONObject(dataStr).getString("RequestUrl");
			Logger.verifyIsNull(requestUrl, "RequestUrl等于空");	    	    	    	
	    }catch (Exception e) {  
	        e.printStackTrace();
	    }  
	}
	
	
	/**
	 * 业务层数据断言（正向）
	 * @param dataStr 返回报文
	 * @param sqlm	sql数据变量
	 * @throws Exception
	 */
	public static void validate_step2(String dataStr, Map sqlm) throws Exception {
	    try {	    	
			//返回报文断言
	    	JSONObject detailReportExportList = new JSONObject(dataStr).getJSONArray("DetailReportExportList").getJSONObject(0);
	    	int sellerId = detailReportExportList.getInt("SellerId");	// 卖家Id
	    	String sellerLoginId = detailReportExportList.getString("SellerLoginId");	// 卖家用户名
	    	
	    	String productId = detailReportExportList.getString("ProductId");	// 商品Id
	    	String productName = detailReportExportList.getString("ProductName");	// 商品名称
	    	Integer categoryId = detailReportExportList.getInt("CategoryId");	// 类目id
	    	String categoryName = detailReportExportList.getString("CategoryName");	// 类目名称
	    	Integer num = detailReportExportList.getInt("Num");	// 商品数量
	    	BigDecimal payCashAmount = (new BigDecimal(detailReportExportList.getDouble("PayCashAmount"))).setScale(2, BigDecimal.ROUND_HALF_UP);	// 支付现金金额
	    	BigDecimal salePrice = (new BigDecimal(detailReportExportList.getDouble("SalePrice"))).setScale(2, BigDecimal.ROUND_HALF_UP);	// 商品卖价
	    	BigDecimal commissionRate = (new BigDecimal(detailReportExportList.getDouble("CommissionRate"))).setScale(2, BigDecimal.ROUND_HALF_UP);	// 佣金比率
	    	BigDecimal minCommissionAmount = (new BigDecimal(detailReportExportList.getDouble("MinCommissionAmount"))).setScale(2, BigDecimal.ROUND_HALF_UP);	// 最小佣金金额限制
	    	BigDecimal maxCommissionAmount = (new BigDecimal(detailReportExportList.getDouble("MaxCommissionAmount"))).setScale(2, BigDecimal.ROUND_HALF_UP);	// 最大佣金金额限制
	    	String remark = detailReportExportList.getString("Remark");	// 备注
	    	int commissionType = detailReportExportList.getInt("CommissionType");	// 佣金类型
	    	BigDecimal specialCommissionRate = (new BigDecimal(detailReportExportList.getDouble("SpecialCommissionRate"))).setScale(2, BigDecimal.ROUND_HALF_UP);	//扣佣、返佣、退佣佣金比率
	    	BigDecimal specialCommissionAmount = (new BigDecimal(detailReportExportList.getDouble("SpecialCommissionAmount"))).setScale(2, BigDecimal.ROUND_HALF_UP);	//扣佣、返佣、退佣佣金金额
	    	String refundCondition = detailReportExportList.getString("RefundCondition");	// 返佣条件
	    	BigDecimal backAmount = (new BigDecimal(detailReportExportList.getDouble("BackAmount"))).setScale(2, BigDecimal.ROUND_HALF_UP);	// 退货退款单退回金额
	    	BigDecimal flightFee = (new BigDecimal(detailReportExportList.getDouble("FlightFee"))).setScale(2, BigDecimal.ROUND_HALF_UP);	// 运费
	    	boolean newSellerFlag = "1".equals(detailReportExportList.getString("NewSellerFlag"));	// 新买手订单标识	
	    	boolean newCustFlag = "1".equals(detailReportExportList.getString("NewCustFlag"));	// 新老客订单标识
	    	
	
    		//SQL取值
    		YmtFundDimensionWapper ymtFundDimensionWapper = new YmtFundDimensionWapper();
    		List<Map> rm = ymtFundDimensionWapper.commissionOrderbyOrderinfoId(sqlm);
    		int sellerIdbysql = (int) rm.get(0).get("SellerId");	// 卖家Id
	    	String sellerLoginIdbysql = (String) rm.get(0).get("SellerLoginId");	// 卖家用户名
	    	String productIdbysql = (String) rm.get(0).get("ProductId");	// 商品Id
	    	String productNamebysql = (String) rm.get(0).get("ProductName");	// 商品名称
	    	Integer categoryIdbysql = (Integer) rm.get(0).get("CategoryId");	// 类目id
	    	String categoryNamebysql = (String) rm.get(0).get("CategoryName");	// 类目名称
	    	Integer numbysql = (Integer) rm.get(0).get("Num");	// 商品数量
	    	BigDecimal payCashAmountbysql = ((BigDecimal) rm.get(0).get("PayCashAmount")).setScale(2, BigDecimal.ROUND_HALF_UP);	// 支付现金金额
	    	BigDecimal salePricebysql = ((BigDecimal) rm.get(0).get("ProductPrice")).setScale(2, BigDecimal.ROUND_HALF_UP);	// 商品卖价
	    	
	    	BigDecimal commissionRatebysql = ((BigDecimal) rm.get(0).get("CommissionRate")).setScale(2, BigDecimal.ROUND_HALF_UP);	// 佣金比率
	    	BigDecimal minCommissionAmountbysql = ((BigDecimal) rm.get(0).get("MinCommissionAmount")).setScale(2, BigDecimal.ROUND_HALF_UP);	// 最小佣金金额限制
	    	BigDecimal maxCommissionAmountbysql = ((BigDecimal) rm.get(0).get("MaxCommissionAmount")).setScale(2, BigDecimal.ROUND_HALF_UP);	// 最大佣金金额限制    	
	    	String remarkbysql = (String) rm.get(0).get("Remark");	// 备注
	    	Short commissionTypebysqlshot = (Short) rm.get(0).get("CommissionType");	// 佣金类型
	    	int commissionTypebysql = (Short)commissionTypebysqlshot;
	    	if(commissionTypebysql==11)		//收佣
	    		{commissionTypebysql=1;}
	    	else if (commissionTypebysql==21)	//退佣
	    		{commissionTypebysql=2;}
	    	else if (commissionTypebysql==12)	//返佣
    		{commissionTypebysql=3;}
	    	BigDecimal specialCommissionRatebysql = ((BigDecimal) rm.get(0).get("SpecialCommissionRate")).setScale(2, BigDecimal.ROUND_HALF_UP);	//扣佣、返佣、退佣佣金比率
	    	BigDecimal specialCommissionAmountbysql = ((BigDecimal) rm.get(0).get("SpecialCommissionAmount")).setScale(2, BigDecimal.ROUND_HALF_UP);	//扣佣、返佣、退佣佣金金额
	    	String refundConditionbysql = (String) rm.get(0).get("RefundCondition");	// 返佣条件
	    	if("1".equals(refundConditionbysql))
	    		{refundConditionbysql="老客订单";}
	    	else if ("2".equals(refundConditionbysql))
	    		{refundConditionbysql="新买手订单";}
	    	else{refundConditionbysql="";}
	    	BigDecimal backAmountbysql; // 退货退款单退回金额
	    	if(rm.get(0).get("BackAmount") != null)
	    		{backAmountbysql = ((BigDecimal) rm.get(0).get("BackAmount")).setScale(2, BigDecimal.ROUND_HALF_UP);}	
	    	else{backAmountbysql = new BigDecimal(0.00).setScale(2, BigDecimal.ROUND_HALF_UP); }	    	
	    	BigDecimal flightFeebysql = ((BigDecimal) rm.get(0).get("FlightFee")).setScale(2, BigDecimal.ROUND_HALF_UP);	// 运费
	    	Boolean newSellerFlagbysql = (Boolean) rm.get(0).get("NewSellerFlag");	// 新买手订单标识    	
	    	Boolean newCustFlagbysql = (Boolean) rm.get(0).get("NewCustFlag");	// 新老客订单标识

    		//断言
	    	Logger.verifyNotNull(detailReportExportList, "主数据非空");
    		Logger.verifyEquals(sellerIdbysql, sellerId, "卖家Id对比");
    		Logger.verifyEquals(sellerLoginIdbysql, sellerLoginId, "卖家用户名对比");
    		Logger.verifyEquals(productIdbysql, productId, "商品Id对比");
    		Logger.verifyEquals(productNamebysql, productName, "商品名称对比");
    		Logger.verifyEquals(categoryIdbysql, categoryId, "类目id对比");
    		Logger.verifyEquals(categoryNamebysql, categoryName, "类目名称对比");
    		Logger.verifyEquals(numbysql, num, "商品数量对比");
    		Logger.verifyEquals(payCashAmountbysql, payCashAmount, "支付现金金额对比");
    		Logger.verifyEquals(salePricebysql, salePrice, "商品卖价对比");
    		Logger.verifyEquals(commissionRatebysql, commissionRate, "佣金比率对比");
    		Logger.verifyEquals(minCommissionAmountbysql, minCommissionAmount, "最小佣金金额限制对比");
    		Logger.verifyEquals(maxCommissionAmountbysql, maxCommissionAmount, "最大佣金金额限制对比");
    		Logger.verifyEquals(remarkbysql, remark, "备注对比");
    		Logger.verifyEquals(commissionTypebysql, commissionType, "佣金类型对比");
    		Logger.verifyEquals(specialCommissionRatebysql, specialCommissionRate, "扣佣、返佣、退佣佣金比率对比");
    		Logger.verifyEquals(specialCommissionAmountbysql, specialCommissionAmount, "扣佣、返佣、退佣佣金金额对比");
    		Logger.verifyEquals(refundConditionbysql, refundCondition, "返佣条件对比");
    		Logger.verifyEquals(backAmountbysql, backAmount, "退货退款单退回金额对比");
    		Logger.verifyEquals(flightFeebysql, flightFee, "运费对比");
    		Logger.verifyEquals(newSellerFlagbysql, newSellerFlag, "新买手订单标识对比");
    		Logger.verifyEquals(newCustFlagbysql, newCustFlag, "新老客订单标识对比");   		
	    	
	    }catch (Exception e) {  
	        e.printStackTrace();
	    }  
	}	
}